import { Component, OnInit} from '@angular/core';
import {Position} from '../../../entity/position';
import {CompanyService} from '../../../service/company.service';
import {Observer} from 'rxjs/Observer';
import {SnackSuccessComponent} from '../../snack-success/snack-success.component';
import {SnackErrorComponent} from '../../snack-error/snack-error.component';
import {SnackService} from '../../../service/snack.service';
import {MatSnackBar, MatDialog} from '@angular/material';
import {DelDialogComponent} from '../del-dialog/del-dialog.component';

@Component({
  selector: 'app-position-list',
  templateUrl: './position-list.component.html',
  styleUrls: [
    './position-list.component.css',
    '../resume-list/resume-list.component.css',
    '../../position-details/position-details-dialog.component.css'
  ]
})
export class PositionListComponent implements OnInit {

  positions: any = new Array<Position>();
  constructor(
    public companyService: CompanyService,
    private snackService: SnackService,
    private snackBar: MatSnackBar,
    public dialog: MatDialog
  ) {
    this.loadData();
  }

  ngOnInit() {
  }
  loadData() {
    let seq = this.companyService.findPositonList();
    seq.subscribe(positions => this.positions = positions);
  }
  delPosition(id: number) {
    const seq = this.companyService.delPosition(id);
    seq.subscribe((res: any) => {
      if (res == 'OK') {
        this.snackService.message = '删除成功';
        this.loadData();
        this.snackBar.openFromComponent(SnackSuccessComponent, {
          duration: 3000,
        });
        // this.returnLogin();
      } else {
      }
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });
  }
  openDialog(id: number): void {
    let dialogRef = this.dialog.open(DelDialogComponent, {
      width: '250px',
    });

    dialogRef.afterClosed().subscribe(result => {
      if (result == 'next') {
        this.delPosition(id);
      }
    });
  }
}
